// pages/yuding/yuding.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
const util = require('../../utils/util.js')
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xuanze: [{
        value: 1,
        name: "暂停",
      },
      {
        value: 0,
        name: "发货",
      },
    ],
    peisong_type: [{
        value: 1,
        name: "门店自取",
      },
      {
        value: 0,
        name: "快递配送",
      },
    ],
    mendian_arr: ['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六'],
    mendian_arrk:['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六'],
    chose_time: 0, //选择时间
    shouhuo_area: '', //地区
    shouhuo_detailarea: '', //详细地址
    shouhuo_name: '', //收货人姓名
    shouhuo_tel: '', // 收货人电话
    addr_id: '', //地址id
    is_ziti: '', //是否自提 0非自提 1自提
    is_zanting: '', //是否暂停发货 0不暂停 1暂停
    peisong_num: 1, //配送数量
    taocan: '',
    order_id: '',
    user_id: wx.getStorageSync('user_id'),
    is_default: '',
    shop_id: '',
    addr: '',
    user_addr: '', //配送地址
    ziti_addr: '', //自提地址 
    user_name: '', //用户名
    user_tel: '', //用户电话
    user_name_ziti:'',
    user_tel_ziti : ''
  },
  updataRadio: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    let that = this;
    that.setData({
      is_zanting: e.detail.value
    })
  },

  updataRadio_peisong_type(envent) {
    console.log('radio发生change事件，携带value值为：', envent.detail.value)
    if(envent.detail.value == 0){
     var mendian_arr = ['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六'];
    }else{
      var mendian_arr = ['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六'];
    }
    let that = this;
    that.setData({
      is_ziti: envent.detail.value,
      mendian_arr:mendian_arr
    })
  },

  bindMendianChange(e) {
    this.setData({
      chose_time: e.detail.value
    })
  },
  goodsNumAdjustCheck(e) {
    let num = e.detail.value
    this.setData({
      peisong_num: num
    })
  },
 
  goodsNumAdjust(e) {
    let peisong_num = this.data.peisong_num;
    if (e.currentTarget.dataset.type == "add") {
      if (peisong_num == this.data.taocan.taocan_yuliang) {
        wx.showToast({
          title: '已达到最高配送量',
          icon: 'none'
        })
      return
      }
      this.setData({
        peisong_num: peisong_num + 1
      })
      
    }
    if (e.currentTarget.dataset.type == "cancel") {
      this.setData({
        peisong_num: peisong_num - 1
      })
      if (peisong_num == 1) {
        wx.showToast({
          title: '最低配送数量为1',
          icon: 'none'
        })
        this.setData({
          peisong_num: 1
        })
      }
    }
  },
  //获取用户信息
  get_userInfo(){
    let that = this;
    common.request(api.get_user_info,{},'post').then((res) =>{
      if(res.status == 1){
        that.setData({
          userInfo:res.userInfo,       
        })
      }
    })
   
  },
  get_MyTaocan() {
    let that = this;
    let uid = wx.getStorageSync('user_id')
    common.request(api.get_MyTaocan, {
      uid: uid
    }, 'post').then((res) => {
      if (res.status == 1) {
        console.log(res)
        if(res.taocan.is_ziti == 0){
         var mendian_arr = ['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六'];
        }else{
          var mendian_arr = ['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六'];
        }
        that.setData({
          is_ziti:res.taocan.is_ziti,
          taocan: res.taocan,
          chose_time: res.taocan.ptime,
          order_id: res.taocan.order_id,
          is_zanting: res.taocan.is_zanting,
          user_addr: res.taocan.user_addr, //配送地址
          ziti_addr: res.taocan.ziti_addr, //自提地址 
          user_name: res.taocan.user_name, //用户名
          user_tel: res.taocan.user_tel, //用户电话
          addr_id:res.taocan.addr_id,
          user_name_ziti:res.taocan.zitiuser_name,
          user_tel_ziti:res.taocan.zitiuser_tel,
          shop_id:res.taocan.shop_id,
          mendian_arr:mendian_arr
        })
      }
    })
  },
  //提交配送信息
  edit_mypeisong() {
    let that = this;
    console.log(that.data.addr_id)
    console.log(that.data.is_ziti)
    console.log(that.data.shop_id)
    if (that.data.is_ziti == 0 && that.data.addr_id == '') {
      wx.showToast({
        title: '请选择配送地址',
        icon: 'none'
      })
      return false
    }
    if (that.data.is_ziti == 1 && that.data.shop_id == '') {
      wx.showToast({
        title: '请添加自取门店',
        icon: 'none'
      })
      return false
    }
 

    if(that.data.chose_time<=0||that.data.chose_time>=8){
      wx.showToast({
        title: '请选择配送时间',
        icon: 'none'
      })
      return false


    }


    // 每日时间段判断

    let time = util.formatTime(new Date());
       console.log('今天周几:', util.getWeekByDate(new Date()))
    let nowweek= util.getWeekByDate(new Date())

//获取当前时间戳 
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
 

//获取当前时间 
var n = timestamp * 1000;
var date = new Date(n);

let nowhours= date.getHours()


console.log('当前时间:',nowhours);



if(that.data.chose_time==nowweek){

  if(nowhours>=9&&that.data.is_ziti==0){ //配送

    Dialog.confirm({
      title: '提示',
      message: '今日下单时间已结束，有效下单时间为当日早9:00前，请重新选择有效配送时间。',
      confirmButtonText:'下周发货',
      cancelButtonText:'重新选择',
      theme: 'round-button',
    })
      .then(() => {
      
        // on confirm
        that.xiadan()
  
  
  
  
      })
      .catch(() => {
        return
        // on cancel
      });
  
  
  }else if(nowhours>=20&&that.data.is_ziti==1){ //自提
  
    Dialog.confirm({
      title: '提示',
      message: '今日下单时间已结束，有效下单时间为当日晚8:00前，请重新选择有效配送时间。',
      confirmButtonText:'下周发货',
      cancelButtonText:'重新选择',
      theme: 'round-button',
    })
      .then(() => {
      
        // on confirm
        that.xiadan()
  
  
  
  
      })
      .catch(() => {
        return
        // on cancel
      });
  
  
  }else{

    that.xiadan()
  }


}else{

  that.xiadan()

}








  },


  xiadan(){
    let that = this;

    let data = {
      order_id: that.data.order_id,
      is_ziti: that.data.is_ziti,
      addr_id: that.data.addr_id||'',
      shop_id: that.data.shop_id||'',
      ptime: that.data.chose_time,
      peisong_num: that.data.peisong_num,
      is_zanting: that.data.is_zanting
    }

    common.request(api.edit_mypeisong, data, 'post').then((res) => {
      if (res.status == 1) {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration:1500
        })
        
        //判断是否完善信息
        if(that.data.userInfo.user_zhiye==''){
          setTimeout(function(){
            wx.navigateTo({
              url: '/pages/member-info/member-info',
            })
          },1500)
        }else{
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/taocan/taocan',
            })
          },1500)
        }
        
       
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //选择收货地址
  choice_address(e) {
    let currentTab=e.currentTarget.dataset.type
    if (this.data.is_ziti == '') {
      wx.showToast({
        title: '请选择配送方式',
        icon: 'none'
      })
    } else {

      wx.navigateTo({
        url: '/pages/address-info/address-info?currentTab='+currentTab,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  

    let that=this
    that.setData({
      user_id: wx.getStorageSync('user_id'),
    })
    const pages = getCurrentPages();
    const currPage = pages[pages.length - 1]; // 当前页
    console.log(currPage.data.mydata_ziti)
    console.log(currPage.data.mydata)
    let newData = currPage.data.mydata_ziti||currPage.data.mydata;


console.log(newData)

if(newData){
  console.log('返回页面')
  let currenttab=newData.currenttab
  console.log(currenttab);
  if (currenttab == 0) {
    that.setData({
      user_addr: newData.shouhuo_area + newData.shouhuo_detailarea,
      user_name: newData.shouhuo_name,
      user_tel: newData.shouhuo_tel,
      addr_id: newData.id,
      currenttab: newData.currenttab,

    })

  }else{

    that.setData({
      ziti_addr: newData.shop_addr,
      shop_id: newData.shop_id,
      shop_name: newData.shop_name,
      user_name_ziti: newData.user_name,
      user_tel_ziti: newData.user_tel,
      currenttab: newData.currenttab,
    })

  }

}else{
  console.log('刚进入页面111')
  this.get_MyTaocan()
  this.get_userInfo()
}


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})